package com.ruoyi.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.otc.domain.BazaarRegulator;
import com.ruoyi.otc.domain.Business;
import com.ruoyi.otc.domain.BusinessTopicLog;
import com.ruoyi.otc.domain.Topic;
import com.ruoyi.otc.service.IBazaarRegulatorService;
import com.ruoyi.otc.service.IBusinessService;
import com.ruoyi.otc.service.IBusinessTopicLogService;
import com.ruoyi.otc.service.ITopicService;
import com.ruoyi.web.api.vo.*;
import com.ruoyi.web.constant.ApiConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Api(value = "企业画像管理接口",tags = "企业画像管理接口")
@RestController
@RequestMapping(ApiConstant.API_V1+"business_topic")
public class ApiBusinessTopicController extends BaseController {

    @Autowired
    private IBusinessTopicLogService businessTopicLogService;
    @Autowired
    private ITopicService topicService;
    @Autowired
    private IBazaarRegulatorService bazaarRegulatorService;
    @Autowired
    private IBusinessService businessService;

    @ApiOperation("前端-查询评测项目列表")
    @PostMapping("/frontTopicList/{id}")
    @ResponseBody
    public AjaxResult frontTopicList(@PathVariable("id") Long id)
    {
        List<BusinessTopicLog> businessTopics = businessTopicLogService.list(new QueryWrapper<BusinessTopicLog>().eq("business_id", id));
        if (businessTopics.size()<=0){
            return AjaxResult.success();
        }
        Set<Long> longs = new HashSet<>();
        for (BusinessTopicLog businessTopic : businessTopics) {
            longs.add(businessTopic.getTopicId());
        }
        List<Topic> list = topicService.list(new QueryWrapper<Topic>().in("id",longs).eq("del",false));
        return AjaxResult.success(list);
    }

    @ApiOperation("查询评测项目列表")
    @PostMapping("/topicList")
    @ResponseBody
    public AjaxResult topicList()
    {
        QueryWrapper<Topic> wrapper = new QueryWrapper<>();
        wrapper.eq("del",false);
        List<Topic> list = topicService.list(wrapper);
        return AjaxResult.success(list);
    }

    /**
     * 查询企业信息列表
     */
    @ApiOperation("查询企业画像管理接口列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody BusinessTopicVo business)
    {
        startPage();
        List<BusinessTopicLog> list = businessTopicLogService.list(new QueryWrapper<BusinessTopicLog>()
                .like(StringUtils.isNotBlank(business.getBusinessName()),"business_name",business.getBusinessName())
                .eq(ObjectUtils.isNotEmpty(business.getTopicId()),"topic_id",business.getTopicId()));
        for (BusinessTopicLog bus : list) {
            List<BazaarRegulator> regulators = bazaarRegulatorService.list(new QueryWrapper<BazaarRegulator>().eq("ent_name", bus.getBusinessName()));
            if (regulators.size()>0){
                bus.setDomDistrict(regulators.get(0).getDomDistrict());
                bus.setIndustryCo(regulators.get(0).getIndustryCo());
            }
        }
        return getDataTable(list);
    }

    /**
     * 删除企业信息
     */
    @ApiOperation("删除企业画像管理信息")
    @PostMapping( "/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id)
    {
        return toAjax(businessTopicLogService.deleteBusinessTopicLogById(id));
    }


    /**
     * 获取企业画像管理信息详情
     */
    @ApiOperation("获取企业画像管理信息详情")
    @PostMapping( "/info/{id}")
    @ResponseBody
    public AjaxResult info(@PathVariable("id") Long id)
    {
        BusinessTopicLog businessTopicLog = businessTopicLogService.getById(id);
        return AjaxResult.success(businessTopicLog);
    }


    /**
     * 前端-企业画像管理-评测问卷-提交问卷
     */
    @ApiOperation("前端-企业画像管理-评测问卷-提交问卷")
    @PostMapping( "/submitTopic")
    @ResponseBody
    public AjaxResult submitTopic(@RequestBody BusinessTopicVo businessTopic)
    {
        BusinessTopicLog log = new BusinessTopicLog();
        BeanUtils.copyProperties(businessTopic,log);
        log.setCreateTime(new Date());
        log.setUpdateTime(new Date());
        Double sum = 0D;
        int count = 0;
        if (businessTopic.getFinanceScore()!=null){
            sum+=businessTopic.getFinanceScore();
            count++;
        }
        if (businessTopic.getMainbodyScore()!=null){
            sum+=businessTopic.getMainbodyScore();
            count++;
        }
        if (businessTopic.getTechnicalityScore()!=null){
            sum+=businessTopic.getTechnicalityScore();
            count++;
        }
        if (businessTopic.getIndependentScore()!=null){
            sum+=businessTopic.getIndependentScore();
            count++;
        }
        if (businessTopic.getOperationScore()!=null){
            sum+=businessTopic.getOperationScore();
            count++;
        }
        log.setSumScore(sum/count);
        businessTopicLogService.save(log);
        businessService.lambdaUpdate().set(Business::getIsTopic,true).eq(Business::getId,businessTopic.getBusinessId()).update();
        return AjaxResult.success();
    }

    @ApiOperation("前端-企业画像管理-评测报告")
    @PostMapping( "/getTopic")
    @ResponseBody
    public AjaxResult getTopic(@RequestBody BusinessTopicReqVo businessTopic)
    {

        List<BusinessTopicLog> list = businessTopicLogService.list(new QueryWrapper<BusinessTopicLog>()
                .eq("topic_id", businessTopic.getTopicId())
                .eq("business_id", businessTopic.getBusinessId())
                .eq("topic_name", businessTopic.getTopicName())
                .orderByDesc("id"));
        if (list.isEmpty()){
            return AjaxResult.success();
        }else{
            BusinessTopicLog topicLog = list.get(0);
            Topic topic = topicService.getById(topicLog.getTopicId());
            topicLog.setSketch(topic.getSketch());
            return AjaxResult.success(topicLog);
        }

    }

    @ApiOperation("前端-企业画像查询")
    @PostMapping( "/getBusinessTopic")
    @ResponseBody
    public AjaxResult getBusinessTopic(@RequestBody BusinessTopicsReqVo reqVo)
    {

        List<BusinessTopicsRespVo> respVoList = businessTopicLogService.getBusinessTopic(reqVo);
        return AjaxResult.success(respVoList);
    }
}
